﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using LevelEditor.Document;

namespace LevelEditor.Forms
{
    public partial class LayerDialog : Form
    {
        public Layer Layer { get; set; }

        public LayerDialog(Layer editLayer)
        {
            InitializeComponent();
            toolTip.SetToolTip(tileSetImageTB, "Filename of image to use for the tileset.");
            toolTip.SetToolTip(resourceNameTB, "A name other than the filename used to identify the tileset; useful for XNA games. Can be left blank.");
            if (editLayer == null)
            {
                Layer = new Layer();
                Layer.Tiles = new List<uint>();
                Layer.TileSet = new TileSet();
                Layer.TileSet.LoadDefaults();
            }
            else
            {
                Layer = editLayer;
                Text = "Edit Layer Options";
                browseButton.Enabled = false;
                browseButton.Visible = false;
                tileSetImageTB.Visible = false;
                resourceNameTB.Enabled = false;
                resourceNameTB.Visible = false;
                tileSetLabel.Text = "Change tileset image via Palette window.";
                resourceNameLabel.Text = "Change resource name via Palette window.";
            }
            tileSetImageTB.Text = Layer.TileSet.ImageFileName;
            resourceNameTB.Text = Layer.TileSet.ResourceName;
            layerWidthNUD.Value = Layer.Size.Width;
            layerHeightNUD.Value = Layer.Size.Height;
            tileWidthNUD.Value = Layer.TileSet.TileSize.Width;
            tileHeightNUD.Value = Layer.TileSet.TileSize.Height;
            xParallaxScaleNUD.Value = Convert.ToDecimal(Layer.ParallaxScale.X);
            yParallaxScaleNUD.Value = Convert.ToDecimal(Layer.ParallaxScale.Y);
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            TileSet ts = Layer.TileSet;
            ts.ImageFileName = tileSetImageTB.Text;
            ts.ResourceName = resourceNameTB.Text;
            Layer.Resize(new Size(Convert.ToInt32(layerWidthNUD.Value), Convert.ToInt32(layerHeightNUD.Value)));
            Layer.TileSet.TileSize = new Size(Convert.ToInt32(tileWidthNUD.Value), Convert.ToInt32(tileHeightNUD.Value));
            Layer.ParallaxScale = new PointF(Convert.ToSingle(xParallaxScaleNUD.Value), Convert.ToSingle(yParallaxScaleNUD.Value));
            DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "png";
            ofd.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG)|*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG|All files (*.*)|*.*";
            DialogResult dr = ofd.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                tileSetImageTB.Text = ofd.FileName;
            }
            ofd.Dispose();
        }
    }
}
